<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class OpenSSL::PKey::DH - openssl: Ruby Standard Library Documentation</title>


<script src="../../js/navigation.js" defer></script>
<script src="../../js/search.js" defer></script>
<script src="../../js/search_index.js" defer></script>
<script src="../../js/searcher.js" defer></script>
<script src="../../js/darkfish.js" defer></script>

<script src="../../js/jquery-3.2.0.min.js"></script>

<script src="../../js/vue.min.js"></script>
<script src="../../js/js.cookie.min.js"></script>

<link href="../../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../../css/rdoc.css" rel="stylesheet">
<link href="../../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../../";
  var index_rel_prefix = "../../";
  var darkModeCsseHref = "../../css/rdoc-dm.css"
  var defaultModeCssHref = "../../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../../table_of_contents.html#pages">Pages</a>
    <a href="../../table_of_contents.html#classes">Classes</a>
    <a href="../../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#class-OpenSSL::PKey::DH-label-Accessor+methods+for+the+Diffie-Hellman+parameters">Accessor methods for the Diffie-Hellman parameters</a>
    <li><a href="#class-OpenSSL::PKey::DH-label-Example+of+a+key+exchange">Example of a key exchange</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link"><a href="PKey.html">OpenSSL::PKey::PKey</a>
</div>

        
<div id="includes-section" class="nav-section">
  <h3>Included Modules</h3>

  <ul class="link-list">
    <li><a class="include" href="../Marshal.html">OpenSSL::Marshal</a>
  </ul>
</div>

        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-generate">::generate</a>
    <li ><a href="#method-c-new">::new</a>
    <li ><a href="#method-i-compute_key">#compute_key</a>
    <li ><a href="#method-i-export">#export</a>
    <li ><a href="#method-i-generate_key-21">#generate_key!</a>
    <li ><a href="#method-i-initialize_copy">#initialize_copy</a>
    <li ><a href="#method-i-params">#params</a>
    <li ><a href="#method-i-params_ok-3F">#params_ok?</a>
    <li ><a href="#method-i-private-3F">#private?</a>
    <li ><a href="#method-i-public-3F">#public?</a>
    <li ><a href="#method-i-public_key">#public_key</a>
    <li ><a href="#method-i-set_key">#set_key</a>
    <li ><a href="#method-i-set_pqg">#set_pqg</a>
    <li ><a href="#method-i-to_der">#to_der</a>
    <li ><a href="#method-i-to_pem">#to_pem</a>
    <li ><a href="#method-i-to_s">#to_s</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-OpenSSL::PKey::DH">
    <h1 id="class-OpenSSL::PKey::DH" class="class">
      class OpenSSL::PKey::DH
    </h1>

    <section class="description">
    
<p>An implementation of the Diffie-Hellman key exchange protocol based on discrete logarithms in finite fields, the same basis that <a href="DSA.html"><code>DSA</code></a> is built on.</p>

<h3 id="class-OpenSSL::PKey::DH-label-Accessor+methods+for+the+Diffie-Hellman+parameters">Accessor methods for the Diffie-Hellman parameters<span><a href="#class-OpenSSL::PKey::DH-label-Accessor+methods+for+the+Diffie-Hellman+parameters">&para;</a> <a href="#top">&uarr;</a></span></h3>
<dl class="rdoc-list note-list"><dt>DH#p
<dd>
<p>The prime (an <a href="../BN.html"><code>OpenSSL::BN</code></a>) of the Diffie-Hellman parameters.</p>
</dd><dt>DH#g
<dd>
<p>The generator (an <a href="../BN.html"><code>OpenSSL::BN</code></a>) g of the Diffie-Hellman parameters.</p>
</dd><dt>DH#pub_key
<dd>
<p>The per-session public key (an <a href="../BN.html"><code>OpenSSL::BN</code></a>) matching the private key. This needs to be passed to <a href="DH.html#method-i-compute_key"><code>DH#compute_key</code></a>.</p>
</dd><dt>DH#priv_key
<dd>
<p>The per-session private key, an <a href="../BN.html"><code>OpenSSL::BN</code></a>.</p>
</dd></dl>

<h3 id="class-OpenSSL::PKey::DH-label-Example+of+a+key+exchange">Example of a key exchange<span><a href="#class-OpenSSL::PKey::DH-label-Example+of+a+key+exchange">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># you may send the parameters (der) and own public key (pub1) publicly</span>
<span class="ruby-comment"># to the participating party</span>
<span class="ruby-identifier">dh1</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">DH</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">2048</span>)
<span class="ruby-identifier">der</span> = <span class="ruby-identifier">dh1</span>.<span class="ruby-identifier">to_der</span>
<span class="ruby-identifier">pub1</span> = <span class="ruby-identifier">dh1</span>.<span class="ruby-identifier">pub_key</span>

<span class="ruby-comment"># the other party generates its per-session key pair</span>
<span class="ruby-identifier">dhparams</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">DH</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">der</span>)
<span class="ruby-identifier">dh2</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span>.<span class="ruby-identifier">generate_key</span>(<span class="ruby-identifier">dhparams</span>)
<span class="ruby-identifier">pub2</span> = <span class="ruby-identifier">dh2</span>.<span class="ruby-identifier">pub_key</span>

<span class="ruby-identifier">symm_key1</span> = <span class="ruby-identifier">dh1</span>.<span class="ruby-identifier">compute_key</span>(<span class="ruby-identifier">pub2</span>)
<span class="ruby-identifier">symm_key2</span> = <span class="ruby-identifier">dh2</span>.<span class="ruby-identifier">compute_key</span>(<span class="ruby-identifier">pub1</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">symm_key1</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">symm_key2</span> <span class="ruby-comment"># =&gt; true</span>
</pre>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-generate" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate(size, generator = 2) &rarr; dh
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates a new <a href="DH.html"><code>DH</code></a> instance from scratch by generating random parameters and a key pair.</p>

<p>See also <a href="../PKey.html#method-c-generate_parameters"><code>OpenSSL::PKey.generate_parameters</code></a> and <a href="../PKey.html#method-c-generate_key"><code>OpenSSL::PKey.generate_key</code></a>.</p>
<dl class="rdoc-list note-list"><dt><code>size</code>
<dd>
<p>The desired key size in bits.</p>
</dd><dt><code>generator</code>
<dd>
<p>The generator.</p>
</dd></dl>

                              <div class="method-source-code" id="generate-source">
            <pre><span class="ruby-comment"># File openssl/lib/openssl/pkey.rb, line 118</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">generate</span>(<span class="ruby-identifier">size</span>, <span class="ruby-identifier">generator</span> = <span class="ruby-value">2</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">blk</span>)
  <span class="ruby-identifier">dhparams</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span>.<span class="ruby-identifier">generate_parameters</span>(<span class="ruby-string">&quot;DH&quot;</span>, {
    <span class="ruby-string">&quot;dh_paramgen_prime_len&quot;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">size</span>,
    <span class="ruby-string">&quot;dh_paramgen_generator&quot;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">generator</span>,
  }, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">blk</span>)
  <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span>.<span class="ruby-identifier">generate_key</span>(<span class="ruby-identifier">dhparams</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-new" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          new &rarr; dh
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(string) &rarr; dh
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(size [, generator]) &rarr; dh
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Creates a new instance of <a href="DH.html"><code>OpenSSL::PKey::DH</code></a>.</p>

<p>If called without arguments, an empty instance without any parameter or key components is created. Use <a href="DH.html#method-i-set_pqg"><code>set_pqg</code></a> to manually set the parameters afterwards (and optionally <a href="DH.html#method-i-set_key"><code>set_key</code></a> to set private and public key components).</p>

<p>If a String is given, tries to parse it as a DER- or PEM- encoded parameters. See also <a href="../PKey.html#method-c-read"><code>OpenSSL::PKey.read</code></a> which can parse keys of any kinds.</p>

<p>The <a href="DH.html#method-c-new"><code>DH.new</code></a>(size [, generator]) form is an alias of <a href="DH.html#method-c-generate"><code>DH.generate</code></a>.</p>
<dl class="rdoc-list note-list"><dt><code>string</code>
<dd>
<p>A String that contains the DER or PEM encoded key.</p>
</dd><dt><code>size</code>
<dd>
<p>See <a href="DH.html#method-c-generate"><code>DH.generate</code></a>.</p>
</dd><dt><code>generator</code>
<dd>
<p>See <a href="DH.html#method-c-generate"><code>DH.generate</code></a>.</p>
</dd></dl>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-comment"># Creating an instance from scratch</span>
<span class="ruby-comment"># Note that this is deprecated and will not work on OpenSSL 3.0 or later.</span>
<span class="ruby-identifier">dh</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">DH</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">dh</span>.<span class="ruby-identifier">set_pqg</span>(<span class="ruby-identifier">bn_p</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">bn_g</span>)

<span class="ruby-comment"># Generating a parameters and a key pair</span>
<span class="ruby-identifier">dh</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">DH</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">2048</span>) <span class="ruby-comment"># An alias of OpenSSL::PKey::DH.generate(2048)</span>

<span class="ruby-comment"># Reading DH parameters</span>
<span class="ruby-identifier">dh_params</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">DH</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-string">&#39;parameters.pem&#39;</span>)) <span class="ruby-comment"># loads parameters only</span>
<span class="ruby-identifier">dh</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span>.<span class="ruby-identifier">generate_key</span>(<span class="ruby-identifier">dh_params</span>) <span class="ruby-comment"># generates a key pair</span>
</pre>

                              <div class="method-source-code" id="new-source">
            <pre>static VALUE
ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
{
    EVP_PKEY *pkey;
    int type;
    DH *dh;
    BIO *in = NULL;
    VALUE arg;

    TypedData_Get_Struct(self, EVP_PKEY, &amp;ossl_evp_pkey_type, pkey);
    if (pkey)
        rb_raise(rb_eTypeError, &quot;pkey already initialized&quot;);

    /* The DH.new(size, generator) form is handled by lib/openssl/pkey.rb */
    if (rb_scan_args(argc, argv, &quot;01&quot;, &amp;arg) == 0) {
        dh = DH_new();
        if (!dh)
            ossl_raise(eDHError, &quot;DH_new&quot;);
        goto legacy;
    }

    arg = ossl_to_der_if_possible(arg);
    in = ossl_obj2bio(&amp;arg);

    /*
     * On OpenSSL &lt;= 1.1.1 and current versions of LibreSSL, the generic
     * routine does not support DER-encoded parameters
     */
    dh = d2i_DHparams_bio(in, NULL);
    if (dh)
        goto legacy;
    OSSL_BIO_reset(in);

    pkey = ossl_pkey_read_generic(in, Qnil);
    BIO_free(in);
    if (!pkey)
        ossl_raise(eDHError, &quot;could not parse pkey&quot;);

    type = EVP_PKEY_base_id(pkey);
    if (type != EVP_PKEY_DH) {
        EVP_PKEY_free(pkey);
        rb_raise(eDHError, &quot;incorrect pkey type: %s&quot;, OBJ_nid2sn(type));
    }
    RTYPEDDATA_DATA(self) = pkey;
    return self;

  legacy:
    BIO_free(in);
    pkey = EVP_PKEY_new();
    if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
        EVP_PKEY_free(pkey);
        DH_free(dh);
        ossl_raise(eDHError, &quot;EVP_PKEY_assign_DH&quot;);
    }
    RTYPEDDATA_DATA(self) = pkey;
    return self;
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-compute_key" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          compute_key(pub_bn) &rarr; string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns a String containing a shared secret computed from the other party’s public value.</p>

<p>This method is provided for backwards compatibility, and calls <a href="PKey.html#method-i-derive"><code>derive</code></a> internally.</p>

<h3 id="method-i-compute_key-label-Parameters">Parameters<span><a href="#method-i-compute_key-label-Parameters">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><em>pub_bn</em> is a <a href="../BN.html"><code>OpenSSL::BN</code></a>, <strong>not</strong> the <a href="DH.html"><code>DH</code></a> instance returned by <a href="DH.html#method-i-public_key"><code>DH#public_key</code></a> as that contains the <a href="DH.html"><code>DH</code></a> parameters only.</p>
</li></ul>

                              <div class="method-source-code" id="compute_key-source">
            <pre><span class="ruby-comment"># File openssl/lib/openssl/pkey.rb, line 49</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">compute_key</span>(<span class="ruby-identifier">pub_bn</span>)
  <span class="ruby-comment"># FIXME: This is constructing an X.509 SubjectPublicKeyInfo and is very</span>
  <span class="ruby-comment"># inefficient</span>
  <span class="ruby-identifier">obj</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">ASN1</span>.<span class="ruby-constant">Sequence</span>([
    <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">ASN1</span>.<span class="ruby-constant">Sequence</span>([
      <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">ASN1</span>.<span class="ruby-constant">ObjectId</span>(<span class="ruby-string">&quot;dhKeyAgreement&quot;</span>),
      <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">ASN1</span>.<span class="ruby-constant">Sequence</span>([
        <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">ASN1</span>.<span class="ruby-constant">Integer</span>(<span class="ruby-identifier">p</span>),
        <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">ASN1</span>.<span class="ruby-constant">Integer</span>(<span class="ruby-identifier">g</span>),
      ]),
    ]),
    <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">ASN1</span>.<span class="ruby-constant">BitString</span>(<span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">ASN1</span>.<span class="ruby-constant">Integer</span>(<span class="ruby-identifier">pub_bn</span>).<span class="ruby-identifier">to_der</span>),
  ])
  <span class="ruby-identifier">derive</span>(<span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">obj</span>.<span class="ruby-identifier">to_der</span>))
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-export" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          export &rarr; aString
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Serializes the <a href="DH.html"><code>DH</code></a> parameters to a PEM-encoding.</p>

<p>Note that any existing per-session public/private keys will <strong>not</strong> get encoded, just the Diffie-Hellman parameters will be encoded.</p>

<p>PEM-encoded parameters will look like:</p>

<pre>-----BEGIN DH PARAMETERS-----
[...]
-----END DH PARAMETERS-----</pre>

<p>See also <a href="PKey.html#method-i-public_to_pem"><code>public_to_pem</code></a> (X.509 SubjectPublicKeyInfo) and <a href="PKey.html#method-i-private_to_pem"><code>private_to_pem</code></a> (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for serialization with the private or public key components.</p>

                              <div class="method-source-code" id="export-source">
            <pre>static VALUE
ossl_dh_export(VALUE self)
{
    OSSL_3_const DH *dh;
    BIO *out;
    VALUE str;

    GetDH(self, dh);
    if (!(out = BIO_new(BIO_s_mem()))) {
        ossl_raise(eDHError, NULL);
    }
    if (!PEM_write_bio_DHparams(out, dh)) {
        BIO_free(out);
        ossl_raise(eDHError, NULL);
    }
    str = ossl_membio2str(out);

    return str;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="DH.html#method-i-to_pem">to_pem</a>, <a href="DH.html#method-i-to_s">to_s</a>
                            </div>

                          </div>

                  <div id="method-i-generate_key-21" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate_key! &rarr; self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Generates a private and public key unless a private key already exists. If this <a href="DH.html"><code>DH</code></a> instance was generated from public DH parameters (e.g. by encoding the result of <a href="DH.html#method-i-public_key"><code>DH#public_key</code></a>), then this method needs to be called first in order to generate the per-session keys before performing the actual key exchange.</p>

<p><strong>Deprecated in version 3.0</strong>. This method is incompatible with <a href="../../OpenSSL.html"><code>OpenSSL</code></a> 3.0.0 or later.</p>

<p>See also <a href="../PKey.html#method-c-generate_key"><code>OpenSSL::PKey.generate_key</code></a>.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-comment"># DEPRECATED USAGE: This will not work on OpenSSL 3.0 or later</span>
<span class="ruby-identifier">dh0</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">DH</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">2048</span>)
<span class="ruby-identifier">dh</span> = <span class="ruby-identifier">dh0</span>.<span class="ruby-identifier">public_key</span> <span class="ruby-comment"># #public_key only copies the DH parameters (contrary to the name)</span>
<span class="ruby-identifier">dh</span>.<span class="ruby-identifier">generate_key!</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">dh</span>.<span class="ruby-identifier">private?</span> <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">dh0</span>.<span class="ruby-identifier">pub_key</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">dh</span>.<span class="ruby-identifier">pub_key</span> <span class="ruby-comment">#=&gt; false</span>

<span class="ruby-comment"># With OpenSSL::PKey.generate_key</span>
<span class="ruby-identifier">dh0</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">DH</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">2048</span>)
<span class="ruby-identifier">dh</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span>.<span class="ruby-identifier">generate_key</span>(<span class="ruby-identifier">dh0</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">dh0</span>.<span class="ruby-identifier">pub_key</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">dh</span>.<span class="ruby-identifier">pub_key</span> <span class="ruby-comment">#=&gt; false</span>
</pre>

                              <div class="method-source-code" id="generate_key-21-source">
            <pre><span class="ruby-comment"># File openssl/lib/openssl/pkey.rb, line 91</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">generate_key!</span>
  <span class="ruby-keyword">if</span> <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">OPENSSL_VERSION_NUMBER</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">0x30000000</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">DHError</span>, <span class="ruby-string">&quot;OpenSSL::PKey::DH is immutable on OpenSSL 3.0; &quot;</span> \
    <span class="ruby-string">&quot;use OpenSSL::PKey.generate_key instead&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">priv_key</span>
    <span class="ruby-identifier">tmp</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span>.<span class="ruby-identifier">generate_key</span>(<span class="ruby-keyword">self</span>)
    <span class="ruby-identifier">set_key</span>(<span class="ruby-identifier">tmp</span>.<span class="ruby-identifier">pub_key</span>, <span class="ruby-identifier">tmp</span>.<span class="ruby-identifier">priv_key</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-initialize_copy" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">initialize_copy</span><span
                                class="method-args">(p1)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="initialize_copy-source">
            <pre>static VALUE
ossl_dh_initialize_copy(VALUE self, VALUE other)
{
    EVP_PKEY *pkey;
    DH *dh, *dh_other;
    const BIGNUM *pub, *priv;

    TypedData_Get_Struct(self, EVP_PKEY, &amp;ossl_evp_pkey_type, pkey);
    if (pkey)
        rb_raise(rb_eTypeError, &quot;pkey already initialized&quot;);
    GetDH(other, dh_other);

    dh = DHparams_dup(dh_other);
    if (!dh)
        ossl_raise(eDHError, &quot;DHparams_dup&quot;);

    DH_get0_key(dh_other, &amp;pub, &amp;priv);
    if (pub) {
        BIGNUM *pub2 = BN_dup(pub);
        BIGNUM *priv2 = BN_dup(priv);

        if (!pub2 || (priv &amp;&amp; !priv2)) {
            BN_clear_free(pub2);
            BN_clear_free(priv2);
            ossl_raise(eDHError, &quot;BN_dup&quot;);
        }
        DH_set0_key(dh, pub2, priv2);
    }

    pkey = EVP_PKEY_new();
    if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
        EVP_PKEY_free(pkey);
        DH_free(dh);
        ossl_raise(eDHError, &quot;EVP_PKEY_assign_DH&quot;);
    }
    RTYPEDDATA_DATA(self) = pkey;
    return self;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-params" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          params &rarr; hash
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Stores all parameters of key to the hash INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!! Don’t use :-)) (I’s up to you)</p>

                              <div class="method-source-code" id="params-source">
            <pre>static VALUE
ossl_dh_get_params(VALUE self)
{
    OSSL_3_const DH *dh;
    VALUE hash;
    const BIGNUM *p, *q, *g, *pub_key, *priv_key;

    GetDH(self, dh);
    DH_get0_pqg(dh, &amp;p, &amp;q, &amp;g);
    DH_get0_key(dh, &amp;pub_key, &amp;priv_key);

    hash = rb_hash_new();
    rb_hash_aset(hash, rb_str_new2(&quot;p&quot;), ossl_bn_new(p));
    rb_hash_aset(hash, rb_str_new2(&quot;q&quot;), ossl_bn_new(q));
    rb_hash_aset(hash, rb_str_new2(&quot;g&quot;), ossl_bn_new(g));
    rb_hash_aset(hash, rb_str_new2(&quot;pub_key&quot;), ossl_bn_new(pub_key));
    rb_hash_aset(hash, rb_str_new2(&quot;priv_key&quot;), ossl_bn_new(priv_key));

    return hash;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-params_ok-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          params_ok? &rarr; true | false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Validates the Diffie-Hellman parameters associated with this instance. It checks whether a safe prime and a suitable generator are used. If this is not the case, <code>false</code> is returned.</p>

<p>See also the man page EVP_PKEY_param_check(3).</p>

                              <div class="method-source-code" id="params_ok-3F-source">
            <pre>static VALUE
ossl_dh_check_params(VALUE self)
{
    int ret;
#ifdef HAVE_EVP_PKEY_CHECK
    EVP_PKEY *pkey;
    EVP_PKEY_CTX *pctx;

    GetPKey(self, pkey);
    pctx = EVP_PKEY_CTX_new(pkey, /* engine */NULL);
    if (!pctx)
        ossl_raise(eDHError, &quot;EVP_PKEY_CTX_new&quot;);
    ret = EVP_PKEY_param_check(pctx);
    EVP_PKEY_CTX_free(pctx);
#else
    DH *dh;
    int codes;

    GetDH(self, dh);
    ret = DH_check(dh, &amp;codes) == 1 &amp;&amp; codes == 0;
#endif

    if (ret == 1)
        return Qtrue;
    else {
        /* DH_check_ex() will put error entry on failure */
        ossl_clear_error();
        return Qfalse;
    }
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-private-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          private? &rarr; true | false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Indicates whether this <a href="DH.html"><code>DH</code></a> instance has a private key associated with it or not. The private key may be retrieved with DH#priv_key.</p>

                              <div class="method-source-code" id="private-3F-source">
            <pre>static VALUE
ossl_dh_is_private(VALUE self)
{
    OSSL_3_const DH *dh;
    const BIGNUM *bn;

    GetDH(self, dh);
    DH_get0_key(dh, NULL, &amp;bn);

#if !defined(OPENSSL_NO_ENGINE)
    return (bn || DH_get0_engine((DH *)dh)) ? Qtrue : Qfalse;
#else
    return bn ? Qtrue : Qfalse;
#endif
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-public-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          public? &rarr; true | false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Indicates whether this <a href="DH.html"><code>DH</code></a> instance has a public key associated with it or not. The public key may be retrieved with DH#pub_key.</p>

                              <div class="method-source-code" id="public-3F-source">
            <pre>static VALUE
ossl_dh_is_public(VALUE self)
{
    OSSL_3_const DH *dh;
    const BIGNUM *bn;

    GetDH(self, dh);
    DH_get0_key(dh, &amp;bn, NULL);

    return bn ? Qtrue : Qfalse;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-public_key" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          public_key &rarr; dhnew
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns a new <a href="DH.html"><code>DH</code></a> instance that carries just the DH parameters.</p>

<p>Contrary to the method name, the returned <a href="DH.html"><code>DH</code></a> object contains only parameters and not the public key.</p>

<p>This method is provided for backwards compatibility. In most cases, there is no need to call this method.</p>

<p>For the purpose of re-generating the key pair while keeping the parameters, check <a href="../PKey.html#method-c-generate_key"><code>OpenSSL::PKey.generate_key</code></a>.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-comment"># OpenSSL::PKey::DH.generate by default generates a random key pair</span>
<span class="ruby-identifier">dh1</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">DH</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">2048</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">dh1</span>.<span class="ruby-identifier">priv_key</span> <span class="ruby-comment">#=&gt; #&lt;OpenSSL::BN 1288347...&gt;</span>
<span class="ruby-identifier">dhcopy</span> = <span class="ruby-identifier">dh1</span>.<span class="ruby-identifier">public_key</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">dhcopy</span>.<span class="ruby-identifier">priv_key</span> <span class="ruby-comment">#=&gt; nil</span>
</pre>

                              <div class="method-source-code" id="public_key-source">
            <pre><span class="ruby-comment"># File openssl/lib/openssl/pkey.rb, line 33</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">public_key</span>
  <span class="ruby-constant">DH</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">to_der</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-set_key" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          set_key(pub_key, priv_key) &rarr; self
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Sets <em>pub_key</em> and <em>priv_key</em> for the <a href="DH.html"><code>DH</code></a> instance. <em>priv_key</em> may be <code>nil</code>.</p>

                            </div>


                          </div>

                  <div id="method-i-set_pqg" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          set_pqg(p, q, g) &rarr; self
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Sets <em>p</em>, <em>q</em>, <em>g</em> to the <a href="DH.html"><code>DH</code></a> instance.</p>

                            </div>


                          </div>

                  <div id="method-i-to_der" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          to_der &rarr; aString
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Serializes the <a href="DH.html"><code>DH</code></a> parameters to a DER-encoding</p>

<p>Note that any existing per-session public/private keys will <strong>not</strong> get encoded, just the Diffie-Hellman parameters will be encoded.</p>

<p>See also <a href="PKey.html#method-i-public_to_der"><code>public_to_der</code></a> (X.509 SubjectPublicKeyInfo) and <a href="PKey.html#method-i-private_to_der"><code>private_to_der</code></a> (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for serialization with the private or public key components.</p>

                              <div class="method-source-code" id="to_der-source">
            <pre>static VALUE
ossl_dh_to_der(VALUE self)
{
    OSSL_3_const DH *dh;
    unsigned char *p;
    long len;
    VALUE str;

    GetDH(self, dh);
    if((len = i2d_DHparams(dh, NULL)) &lt;= 0)
        ossl_raise(eDHError, NULL);
    str = rb_str_new(0, len);
    p = (unsigned char *)RSTRING_PTR(str);
    if(i2d_DHparams(dh, &amp;p) &lt; 0)
        ossl_raise(eDHError, NULL);
    ossl_str_adjust(str, p);

    return str;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-to_pem" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          to_pem &rarr; aString
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Serializes the <a href="DH.html"><code>DH</code></a> parameters to a PEM-encoding.</p>

<p>Note that any existing per-session public/private keys will <strong>not</strong> get encoded, just the Diffie-Hellman parameters will be encoded.</p>

<p>PEM-encoded parameters will look like:</p>

<pre>-----BEGIN DH PARAMETERS-----
[...]
-----END DH PARAMETERS-----</pre>

<p>See also <a href="PKey.html#method-i-public_to_pem"><code>public_to_pem</code></a> (X.509 SubjectPublicKeyInfo) and <a href="PKey.html#method-i-private_to_pem"><code>private_to_pem</code></a> (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for serialization with the private or public key components.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="DH.html#method-i-export">export</a>
                            </div>
                          </div>

                  <div id="method-i-to_s" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          to_s &rarr; aString
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Serializes the <a href="DH.html"><code>DH</code></a> parameters to a PEM-encoding.</p>

<p>Note that any existing per-session public/private keys will <strong>not</strong> get encoded, just the Diffie-Hellman parameters will be encoded.</p>

<p>PEM-encoded parameters will look like:</p>

<pre>-----BEGIN DH PARAMETERS-----
[...]
-----END DH PARAMETERS-----</pre>

<p>See also <a href="PKey.html#method-i-public_to_pem"><code>public_to_pem</code></a> (X.509 SubjectPublicKeyInfo) and <a href="PKey.html#method-i-private_to_pem"><code>private_to_pem</code></a> (PKCS #8 PrivateKeyInfo or EncryptedPrivateKeyInfo) for serialization with the private or public key components.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="DH.html#method-i-export">export</a>
                            </div>
                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

